﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using DOAN.DO;
using DOAN.BL;

namespace DOAN
{
    public partial class QuenMK : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public static string CreateRandomPassword(int PasswordLength)
        {
            string _allowedChars = "abcdefghijkmnopqrstuvwxyz0123456789";
            Random randNum = new Random();
            char[] chars = new char[PasswordLength];
            int allowedCharCount = _allowedChars.Length;
            for (int i = 0; i < PasswordLength; i++)
            {
                chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
            }
            return new string(chars);
        }

        public void sendsMail(String name, String bd, string title)
        {
            string i = DateTime.Now.ToString();


            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential("thuyetlv@ses.vn", "hthuyeet2435");
            SmtpServer.Port = 587;
            SmtpServer.Host = "Smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            MailMessage mail = new MailMessage();
            String addr = txtemail1.Text.ToString();

            try
            {
                //mail.From = new MailAddress(
                mail.From = new MailAddress(addr, name, System.Text.Encoding.UTF8);
                mail.To.Add(addr);
                mail.Subject = title;
                mail.Body = bd;
                mail.IsBodyHtml = true;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                SmtpServer.Send(mail);
                Response.Write("<script>alert('Send email successful " + i + ";!!!')</script>");

            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('Loi " + ex.Message + ";!!!')</script>");
            }
        }

        protected void btndongy_Click(object sender, EventArgs e)
        {
            {
                ccJoin.ValidateCaptcha(TextBox1.Text);
                if (!ccJoin.UserValidated)
                {
                    Label3.Text = "Mã bảo vệ chưa chính xác";
                    return;
                }
                else
                {
                    Label3.Text = "Thành công";
                }


            }
            DataTable dt = new ThanhvienBL().SelectByEmail(txtemail1.Text);
            ThanhvienDO obj = new ThanhvienBL().SelectobjByEmail(txtemail1.Text); 
            if ((obj.Tentaikhoan != "")&&(obj.Tentaikhoan != null))
            {
                string pass = CreateRandomPassword(7);
                obj.Matkhau = pass;
                int kq = new ThanhvienBL().Update(obj);
                if(kq==0)
                    sendsMail("Admin", "Mật khẩu đăng nhập của bạn là: " + pass, "Cấp lại pass");
            }
            else
            {
                Response.Write("<script>alert('Email của bạn chưa được đăng ký;!!!')</script>");
            }
        }

        
    }
}
